<?php

namespace App\Rep;

use App\Models\ExchangeGoods;
use App\Models\FinishProduct;
use App\Models\Maintain;
use App\Models\Order;
use App\Models\OrderLogistics;
use App\Models\OrderSecond;
use App\Models\ReturnedGoods;
use App\Models\SemiFinishProduct;
use Illuminate\Support\Facades\DB;

class ReturnedGoodsRep
{

    /**
     * 获取退货列表
     * @param $params
     * @return void
     */
    public function getReturnedGoodsListRep($params){
        $list = ReturnedGoods::Search($params)->with(['order'])->paginate($params['per_page']??10);
        return $list;
    }

    /**
     * 新增退货
     * @param $params
     * @return void
     */
    public function addReturnedGoodsRep($params){
        DB::beginTransaction();
        try{
            $order = Order::where('id',$params['order_id'])->first();
            $finish = FinishProduct::where('id',$params['finish_id'])->first();
            $semi_id_arr = SemiFinishProduct::where('finish_id',$finish)->pluck('id')->toArray();
            $model = new ReturnedGoods();
            $model->order_id = $params['order_id'];
            $model->remark = $params['remark'];
            $model->returned_time = $params['returned_time'];
            $model->returned_position = $params['returned_position'];
            $model->customer_id = $order->customer_id;
            $model->semi_ids = implode(',',$semi_id_arr);
            $model->save();
            FinishProduct::where('order_id',$params['order_id'])->update(['return_status'=>1]);
            OrderLogistics::where('order_id',$params['order_id'])->update(['is_del'=>1]);
            if($params['returned_position'] == 1){
                //退回成品库
                $semiModel = SemiFinishProduct::where('finish_id',$finish->id)->get();
                foreach($semiModel as $k=>$v){
                    $v->finish_id = '';
                    $v->is_del = 2;
                    $v->save();
                }
            }elseif($params['returned_position'] == 2){
                //退回维修库
                $semiModel = SemiFinishProduct::where('finish_id',$finish->id)->get();
                foreach($semiModel as $k=>$v){
                    $maintainModel = new Maintain();
                    $maintainModel->order_id = $order->id;
                    $maintainModel->semi_id = $v->id;
                    $maintainModel->remark = $params['remark'];
                    $maintainModel->save();
                }

            }
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>'success'];
        }catch(\Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$exception->getMessage()];
        }


    }


    /**
     * 获取已发货的订单列表
     * @param $params
     * @return void
     */
    public function getDeliveredOrderListRep($params){
        $finishRep = new FinishProductRep();
        $params['logistics_status'] = 3;
        $params['return_status'] = 0;
        $list = $finishRep->getFinishProductListRep($params);
        return $list;
    }

    /**
     * 换货
     * @param $params
     * @return array
     */
    public function exchangeGoodsRep($params){
        DB::beginTransaction();
        try{
            //当前半成品
            $semi = SemiFinishProduct::where('id',$params['semi_id'])->first();

            $model = new ExchangeGoods();
            $model->order_id = $semi->order_id;
            $model->exchange_time = $params['exchange_time'];
            $model->remark = $params['remark'];
            $model->semi_id = $params['semi_id'];
            $model->num = $params['num'];
            $semiRep = new SemiFinishProductRep();
            //从库存中随机取出
            $semi_id_arr = $semiRep->getInventoryIdRep($semi->product_id,$semi->model_number_id,$semi->specification_id,$warehouse_id,$goods_shelf_id);
            $key = array_rand($semi_id_arr);
            $val = $semi_id_arr[$key];
            $model->semi_id_now = $val;
            $model->save();
            $semi_now = SemiFinishProduct::where('id',$val)->first();
            $semi_now->finish_id = $semi->finish_id;
            $semi_now->save();
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>'success'];
        }catch(\Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$exception->getMessage()];
        }


    }

}
